package lock.mysql;

/**
 * @author 徒有琴
 */
public class BuyTest {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 20; i++) {
            int num = i % 3 + 1;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Buyer b1 = new Buyer();
                        int[] res = b1.getAmount();//检查库存
                        if (res[0] >= num) {
                            if(b1.updateAmount(num, res[1])>0){
                                System.out.println(Thread.currentThread().getName() + "购买了" + num);
                            }else{
                                //重试
                            }
                        } else {
                            System.out.println("库存不足");
                            b1.release();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }
}
